package com.wpen.unit.web.controller.system;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.wpen.unit.commun.BaseResponse;
import com.wpen.unit.security.Base64;
import com.wpen.unit.service.system.UserService;
import com.wpen.unit.web.WebVariables;
import com.wpen.unit.web.dto.system.LoginRequest;
import com.wpen.unit.web.dto.system.LoginResponse;

import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "登录登出服务接口")
@RestController
@RequestMapping("/api/public")
public class LoginController {
//    private static String RME_ID = "RMESESSION";
    @Value("${http.session.timeout:120000}")
    private long sessionTimeout;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserService userService;

    @PostMapping(value = "/login", produces = MediaType.APPLICATION_JSON_VALUE)
    public LoginResponse login(@RequestBody @Valid LoginRequest loginParam) throws Exception {
        // 上送数据验证
        String pwdStr = Base64.decodeToStr(loginParam.getEnPwd());
        return userService.userLogin(request, loginParam.getUserName(), pwdStr);
    }

    @RequestMapping(value = "/unlogin", method = RequestMethod.GET)
    public Object unlogin() {
        BaseResponse resp = new BaseResponse();
        resp.setCode("999999");
        resp.setMsg("not login");
        return resp;
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public BaseResponse logout(HttpServletRequest request, HttpServletResponse response) throws Throwable {
        String token = request.getHeader(WebVariables.REQUEST_HEAD_AUTH);
        request.getSession().removeAttribute(token);

        BaseResponse resp = new BaseResponse();
        resp.setMsg("logout");
        return resp;
    }

}
